package src.controller;

import java.rmi.RemoteException;

import javax.swing.DefaultListModel;

import src.domain.MailBox;
import src.resource.CommonMethods;
import src.resource.DialogResource;
import src.resource.Md5Resource;
import src.resource.ValidateResource;
import src.resource.DialogResource.windowTypeEnum;
import src.service.ClientService;
import src.view.ManageMailBoxes;

public class ManageMailBoxesController {

	private ClientService system;
	private ManageMailBoxes view;

	public ManageMailBoxesController(ClientService system) {
		this.view = ManageMailBoxes.getInstance(this);
		this.system = system;
	}

	public void performSearchMailBox() throws RemoteException {
		this.cleanForm();
		this.view.getListModelMailBoxes().removeAllElements();
		view.setListModelMailBoxes(CommonMethods.toListModel(system.getMailBoxes(view.getTxtSearch().getText())));
		view.getjListMailBoxes().setModel(view.getListModelMailBoxes());
	}

	private void cleanForm() {
		view.getTxtName().setText("");
		view.getjPasswordFieldConfirmPass().setText("");
		view.getjPasswordFieldPass().setText("");
		view.getChkActive().setSelected(false);
	}
	
	public void performDeleteMailBox() throws RemoteException{
		if(view.getjListMailBoxes().getSelectedValue()==null){
			DialogResource.showMessage(view.getBtnDelete().getLocationOnScreen(),"Information","Must select a mailbox.",windowTypeEnum.wtInformation);
			return;
		}
		
		if(system.getOutgoingMessagesByMailBox(((MailBox)this.view.getjListMailBoxes().getSelectedValue()).getId()).size()!=0){
			DialogResource.showMessage(view.getBtnDelete().getLocationOnScreen(),"Information","You can not delete. This mailbox has outgoing messages.",windowTypeEnum.wtInformation);
			return;
		}
		
		system.deleteMailBox(((MailBox)view.getjListMailBoxes().getSelectedValue()).getId());
		DialogResource.showMessage(view.getBtnDelete().getLocationOnScreen(),"Information","Mailbox deleted sucessfully.",windowTypeEnum.wtConfirmation);
		this.cleanForm();
		this.view.getTxtSearch().setText("");
		this.view.getListModelMailBoxes().removeAllElements();
	}
	
	@SuppressWarnings("deprecation")
	public void performUpdateMailBox() throws RemoteException{
		if(!validateRequiredFields()){
			DialogResource.showMessage(view.getBtnSave().getLocationOnScreen(),"Information","Must complete the required fields.",windowTypeEnum.wtInformation);
			return;
		}
		
		if(!validateFieldsLenght()){
			DialogResource.showMessage(view.getBtnSave().getLocationOnScreen(),"Information","The fields can not contain more than 20 characters.",windowTypeEnum.wtInformation);
			return;
		}
		
		/*if(system.getMailBoxByName(view.getTxtName().getText())!=null){
			DialogResource.showMessage(view.getBtnSave().getLocationOnScreen(),"Information","Name in use. Please enter another name.",windowTypeEnum.wtInformation);
			return;
		}*/
		
		if(!view.getjPasswordFieldPass().getText().equals(view.getjPasswordFieldConfirmPass().getText())){
			DialogResource.showMessage(view.getBtnSave().getLocationOnScreen(),"Information","The password does not match.",windowTypeEnum.wtInformation);
			return;
		}
		
		if(view.getjListMailBoxes().getSelectedValue()==null){
			DialogResource.showMessage(view.getBtnSave().getLocationOnScreen(),"Information","Must select a mailbox.",windowTypeEnum.wtInformation);
			return;
		}
		
		((MailBox)this.view.getjListMailBoxes().getSelectedValue()).setName(view.getTxtName().getText());
		((MailBox)this.view.getjListMailBoxes().getSelectedValue()).setPass(Md5Resource.encriptarmd5(view.getjPasswordFieldPass().getText()));
		((MailBox)this.view.getjListMailBoxes().getSelectedValue()).setActive(view.getChkActive().isSelected());
			
		system.updateMailBox((MailBox)view.getjListMailBoxes().getSelectedValue());
		this.cleanForm();
		this.view.getTxtSearch().setText("");
		((DefaultListModel)this.view.getListModelMailBoxes()).removeAllElements();
	}
	
	@SuppressWarnings("deprecation")
	private boolean validateRequiredFields(){
		return ValidateResource.validateRequiredField(view.getTxtName().getText()) && ValidateResource.validateRequiredField(view.getjPasswordFieldPass().getText())
		&& ValidateResource.validateRequiredField(view.getjPasswordFieldConfirmPass().getText());
	}
	
	@SuppressWarnings("deprecation")
	private boolean validateFieldsLenght(){
		return ValidateResource.validateStringLength(view.getTxtName().getText(), 20) && ValidateResource.validateStringLength(view.getjPasswordFieldPass().getText(), 20)
		&& ValidateResource.validateStringLength(view.getjPasswordFieldPass().getText(), 20);
	}
	
	private void bindMailBox(){
		MailBox m=(MailBox)view.getjListMailBoxes().getSelectedValue();
		view.getTxtName().setText(m.getName());
		view.getChkActive().setSelected(m.isActive());
	}
	
	public void performSelectItem() throws RemoteException{
		if(view.getjListMailBoxes().getSelectedValue()==null)
			return;
		this.bindMailBox();
		
	}
}
